import { _decorator, CCBoolean, CCString, Enum, RenderTexture } from 'cc';
import { SensorComponent } from './SensorComponent';
import { StackingSensor } from './StackingSensor';
import { RenderTextureSensor } from './RenderTextureSensor';
import { SensorCompressionType } from './CompressionSpec';
import { ISensor } from './ISensor';
const { ccclass, property, menu } = _decorator;


Enum(SensorCompressionType);

@ccclass('RenderTextureSensorComponent')
@menu("ML Agents/Render Texture Sensor")
export class RenderTextureSensorComponent extends SensorComponent {

    private _sensor: RenderTextureSensor = null!;

    @property({ type: RenderTexture, serializable: true, visible: true })
    private _renderTexture: RenderTexture = null!;
    public get renderTexture(): RenderTexture {
        return this._renderTexture;
    }
    public set renderTexture(value: RenderTexture) {
        this._renderTexture = value;
    }


    @property(CCString)
    sensorName: string = 'RenderTextureSensor';

    @property({ serializable: true, visible: true })
    private _grayScale: boolean = false;
    public get grayScale(): boolean {
        return this._grayScale;
    }
    public set grayScale(value: boolean) {
        this._grayScale = value;
    }


    @property({ range: [1, 50, 1] })
    public observationStacks = 1;


    @property({ type: SensorCompressionType, serializable: true, visible: true })
    private _compression: SensorCompressionType = SensorCompressionType.PNG;
    public get compression(): SensorCompressionType {
        return this._compression;
    }
    public set compression(value: SensorCompressionType) {
        this._compression = value;
        this.updateSensor();
    }



    createSensors(): ISensor[] {
        this._sensor = new RenderTextureSensor(this.renderTexture, this.grayScale, this.sensorName, this._compression);
        if (this.observationStacks !== 1) {
            return [new StackingSensor(this._sensor, this.observationStacks)]
        }

        return [this._sensor];
    }

    private updateSensor() {
        if (this._sensor != null) {
            this._sensor.compressionType = this._compression;
        }
    }

    getSensor(): ISensor {
        return this._sensor;
    }

    protected onDestroy(): void {
        this._sensor = null!;
    }


}